package io.bdmc.common.utils;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;


public class GeoUtil {
    
    public static double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo, Ellipsoid ellipsoid) {
        // 创建GeodeticCalculator，调用计算方法，传入坐标系、经纬度用于计算距离
        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo);
        return geoCurve.getEllipsoidalDistance();
    }

    /**
     * 计算2点间的距离，返回double
     * @param startLat
     * @param startLng
     * @param endLat
     * @param endLng
     * @return
     */
    public static double getDistanceMeter(String startLat,String startLng,String endLat,String endLng) {
        GlobalCoordinates from = new GlobalCoordinates(Double.parseDouble(startLat), Double.parseDouble(startLng));
        GlobalCoordinates to = new GlobalCoordinates(Double.parseDouble(endLat), Double.parseDouble(endLng));
        return getDistanceMeter(from,to,Ellipsoid.WGS84);
    }

     /**
     * 计算2点间的距离，返回String
     * @param startLat
     * @param startLng
     * @param endLat
     * @param endLng
     * @return
     */
    public static String getDistanceMeterStr(String startLat,String startLng,String endLat,String endLng) {
        GlobalCoordinates from = new GlobalCoordinates(Double.parseDouble(startLat), Double.parseDouble(startLng));
        GlobalCoordinates to = new GlobalCoordinates(Double.parseDouble(endLat), Double.parseDouble(endLng));
        double disKM = getDistanceMeter(from,to,Ellipsoid.WGS84)/1000;
        String strKM = String.format("%.2f", disKM);
        return strKM;
    }
}